function [length,X1,X2] = computeCrackLength(f,center,normal,PosDomain,NegDomain)
%% realise 2 edges that the crack cut by
np = size(PosDomain(:),1);
if np == 1
    cnt = 1;
    for i=1:3
        if isAdjacent(PosDomain,NegDomain(i))
            a(cnt) = NegDomain(i);
            cnt = cnt+1;
        end
    end
    edge1 = [PosDomain a(1)];
    edge2 = [PosDomain a(2)];
elseif np == 2
    %if abs(PosDomain(1) - NegDomain(1)) == 2
    if ~isAdjacent(PosDomain(1),NegDomain(1))
        edge1 = [PosDomain(1) NegDomain(2)];
        edge2 = [PosDomain(2) NegDomain(1)];
    else
        edge1 = [PosDomain(1) NegDomain(1)];
        edge2 = [PosDomain(2) NegDomain(2)];
    end
elseif np == 3
    cnt = 1;
    for i=1:3
        if isAdjacent(NegDomain,PosDomain(i))
            a(cnt) = PosDomain(i);
            cnt = cnt+1;
        end
    end
    edge1 = [NegDomain a(1)];
    edge2 = [NegDomain a(2)];
else
    disp('something go wrong with the domain');
end

%% compute intersection with edge 1
c = -normal(1)*center(1) - normal(2)*center(2);
nodes = f.getNodes();
n1 = nodes(edge1(1)).getPosition();
n2 = nodes(edge1(2)).getPosition();
xi1 = n1(1); yi1 = n1(2);
xi2 = n2(1); yi2 = n2(2);
a1 = yi2 - yi1;
b1 = xi1 - xi2;
c1 = yi1 * (xi2 - xi1) - xi1 * (yi2 - yi1);
X1 = computeIntersection(normal(1),normal(2),c,a1,b1,c1);

%% compute intersection with edge 2
n1 = nodes(edge2(1)).getPosition();
n2 = nodes(edge2(2)).getPosition();
xi1 = n1(1); yi1 = n1(2);
xi2 = n2(1); yi2 = n2(2);
a1 = yi2 - yi1;
b1 = xi1 - xi2;
c1 = yi1 * (xi2 - xi1) - xi1 * (yi2 - yi1);
X2 = computeIntersection(normal(1),normal(2),c,a1,b1,c1);

%% compute length
length = norm(X1-X2);

end

function y = isAdjacent(n1,n2)
if abs(n1-n2)==2
    y = 0;
else
    y = 1;
end
end
